package Backtracking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
* 17. 电话号码的字母组合
*
* dfs+回溯
* */
public class LetterCombinations17 {
    List<String> list=new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0){
            return list;
        }
        Map<Character, String> map = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backTracking(map,digits,0,new StringBuffer());
        return list;
    }

    private void backTracking(Map<Character, String> map, String digits, int index, StringBuffer sb) {
        if(index==digits.length()){
            list.add(sb.toString());
            return;
        }
        char ch = digits.charAt(index);
        String letter = map.get(ch);
        for(int i=0;i<letter.length();i++){
            sb.append(letter.charAt(i));
            backTracking(map, digits, index+1, sb);
            sb.deleteCharAt(index);
        }
    }
}
